package com.efruit.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.efruit.externalInterfaces.IProductInfoMag;
import com.efruit.productInfoMag.IProductInfo;
import com.efruit.struts.form.ToUpdateProductInfoForm;

public class ToUpdateProductInfoAction extends Action{
	IProductInfoMag productInfoMag = null;
	
	public IProductInfoMag getProductInfoMag() {
		return productInfoMag;
	}

	public void setProductInfoMag(IProductInfoMag productInfoMag) {
		this.productInfoMag = productInfoMag;
	}
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println("enter UpdateProductInfoAction");
		ToUpdateProductInfoForm updateForm = (ToUpdateProductInfoForm)form;
		System.out.println(updateForm.getId());
		try {
			request.setAttribute("updateId",String.valueOf(updateForm.getId()));
			IProductInfo tmp =productInfoMag.getProductInfo(updateForm.getId());
			if( tmp != null){
				request.setAttribute("updateProductName",tmp.getProductName());
				request.setAttribute("updateProductUrl",tmp.getUrl());
				request.setAttribute("updateProductCount",String.valueOf(tmp.getCount()));
				System.out.println("zhaowei : "+tmp.getPrice());
				request.setAttribute("updateProductPrice",String.valueOf(tmp.getPrice()));
			}else{
				request.setAttribute("updateProductName","");
				request.setAttribute("updateProductUrl","");
				request.setAttribute("updateProductCount","");
				request.setAttribute("updateProductPrice ","");
			}
			System.out.println("after UpdateProductInfoAction");
			return mapping.findForward("updateProductInfo");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
	}
}
